package com.pronhub.cloud.shop.web.controller;

import com.pronhub.cloud.shop.redis.MyRedisTemplate;
import com.pronhub.cloud.shop.redis.SeckillKeyPrefix;
import com.pronhub.cloud.shop.util.VerifyCodeImgUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * 那个验证码控制的 一个算式题的
 */
@Controller
@RequestMapping("/verifyCode")
public class VerifyCodeController {
    @Autowired
    private MyRedisTemplate myRedisTemplate;

    @RequestMapping("/getVerifyCode")
    @ResponseBody
    public void getVerifyCode(String uuid,HttpServletResponse response) throws IOException {
        //获得数值
        String verifyCode = VerifyCodeImgUtil.generateVerifyCode();
        Integer result = VerifyCodeImgUtil.calc(verifyCode);
        //放入Redis
        myRedisTemplate.set(SeckillKeyPrefix.VERIFY_CODE_RESULT,uuid,result);
        //生产图片
        BufferedImage verifyCodeImg = VerifyCodeImgUtil.createVerifyCodeImg(verifyCode);
        //返回页面
        ImageIO.write(verifyCodeImg,"JPEG",response.getOutputStream());//把生成出来的图片通过response响应到客户端

    }
}
